﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NDBGen
{
    public class ScriptedDataColumn
    {
        public String Name { get; private set; }
        public Boolean RequiresValueLookup { get; private set; }
        public Boolean TriggersBulkInsert { get; set; }
        public String ReferencedTableSchema { get; private set; }
        public String ReferencedTableName { get; private set; }
        public String ReferencedTableLookupColumn { get; private set; }
        public String ReferencedTableValueColumn { get; private set; }
        public Boolean IsLookupKey { get; set; }
        public String ParameterName { get { return this.Name.Replace("_id", String.Empty); } }

        public String BulkInsertSuffix
        {
            get { return String.Format("__for__{0}_{1}_{2}", this.ReferencedTableSchema, this.ReferencedTableName.Depluralize(), this.ReferencedTableValueColumn); }
        }

        public static ScriptedDataColumn[] Parse(String[] columnDefs)
        {
            var list = new List<ScriptedDataColumn>(columnDefs.Length);

            foreach (var columnDef in columnDefs)
            {
                var lookupColumn = new ScriptedDataColumn();
                lookupColumn.IsLookupKey = columnDef.Contains("!");

                var parts = columnDef.Split('>');
                lookupColumn.Name = parts[0].Trim('!');

                if (parts.Length > 1)
                {
                    var refParts = parts[1].Trim('!').Split('.');
                    lookupColumn.ReferencedTableSchema = refParts[0];
                    lookupColumn.ReferencedTableName = refParts[1];

                    var refColumnsParts = refParts[2].Split('=');
                    if (refColumnsParts.Length > 1)
                    {
                        lookupColumn.RequiresValueLookup = true;
                        lookupColumn.ReferencedTableLookupColumn = refColumnsParts[0];
                        lookupColumn.ReferencedTableValueColumn = refColumnsParts[1];
                    }
                    else
                    {
                        lookupColumn.TriggersBulkInsert = true;
                        lookupColumn.ReferencedTableValueColumn = refColumnsParts[0];
                    }
                }

                list.Add(lookupColumn);
            }

            return list.ToArray();
        }

        public String GetParameterType(List<Column> list)
        {
            return "nvarchar(max)";
            /*var targetColumn = list.Single(c => c.Name == this.ReferencedTableLookupColumn);
            return String.Format("{0}{1}",
                targetColumn.DataType.ToString().Replace("Max", "(max)").ToLower(),
                targetColumn.Length.HasValue ? String.Format("({0})", targetColumn.Length) : String.Empty);*/
        }

        public override String ToString()
        {
            var b = new StringBuilder(this.Name);
            if (this.IsLookupKey) b.Append('!');
            if (this.ReferencedTableSchema != null)
            {
                b.Append('>');
                b.Append(this.ReferencedTableSchema);
                b.Append('.');
                b.Append(this.ReferencedTableName);
                b.Append('.');
                b.Append(this.ReferencedTableLookupColumn);
                b.Append('=');
                b.Append(this.ReferencedTableValueColumn);
            }

            return b.ToString();
        }
    }
}